<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Default_Model_NewsCategory extends ZF_DB{
    protected $_table_name = "news_category" ;
    protected $_table_news = "news";
    protected $_table_category = "category";
    
    protected $_id;
    protected $_id_news;
    protected $_id_category;
    
    
    public function getIdNews(){
        return $this->_id_news;
    }
    public function setIdNews($id_news){
        $this->_id_news = $id_news;
    }
    public function getIdCategory(){
        return $this->_id_category;
    }
    public function setIdCategory($id_category){
        $this->_id_category = $id_category;
    }
    
    public function getId(){
        return $this->_id;
    }
    public  function setId($id){
        $this->_id = $id;
    }
   

 
    
    public function insert($id_news,
            $id_category){
        
        if($id_news && $id_category){ 
                if($this->checkExist($id_news, $id_category)){
                    $data =  array(
                        'id_category'=>$id_category,
                        'id_news' =>$id_news,
                        );
                return         $this->db->insert($this->_table_name,$data);
            }
        }
        return -1;	
    }
    public function checkExist($id_news,$id_category){
         $select = $this->db->select()
                ->from(array("p"=>$this->_table_name))
                ->where("p.id_news = ?",$id_news)
                ->where("p.id_category = ?",$id_category);
	$data = $this->db->fetchAll($select);
        if($data){
            return 1;
        }
	return -1;
    }
    public function getById($id){
        if($id)
	{
		$select = $this->db->select()
			->from(array("p"=>$this->_table_name))
                         ->join(array("p1"=>$this->_table_news),
								"p.id_news = p1.id")
						->join(array("p2"=>$this->_table_category),
								"p.id_category = p2.id",
								array($this->_table_category."_name"=>"name"))
                        ->where("p.id = ?",$id )
                        ->where("active = ?",1) ;
		
		$data = $this->db->fetchRow($select);
		if($data){
			$this->_id = $data['id'];
			
                        $this->_id_news = $data['id_news'];
                        $this->_id_category = $data['id_category'];
			
		}
                return $data;
	}
	return -1;
    }
   public function getByIdNews($id_news,$limit = 0,$offset = 0,$orderby = "timestamp"){
        if($id_news)
	{
		$select = $this->db->select()
			->from(array("p"=>$this->_table_name))
                        ->join(array("p1"=>$this->_table_news),
								"p.id_news = p1.id")
						->join(array("p2"=>$this->_table_category),
								"p.id_category = p2.id",
								array($this->_table_category."_name"=>"name"))
                        ->where("p.id_news = ?",$id_news )
                        ->where("active = ?",1)
                        ->limit($limit,$offset)
                        ->order("$orderby DESC");
		
		$data = $this->db->fetchAll($select);
		
                return $data;
	}
	return -1;
    }
    public function getByIdCategory($id_category,$limit = 0,$offset = 0,$orderby = "timestamp"){
        if($id_category)
	{
		$select = $this->db->select()
			->from(array("p"=>$this->_table_name))
                        ->join(array("p1"=>$this->_table_news),
								"p.id_news = p1.id")
						->join(array("p2"=>$this->_table_category),
								"p.id_category = p2.id",
								array($this->_table_category."_name"=>"name"))
                        ->where("p.id_category = ?",$id_category )
                        ->where("p1.active = ?",1)
                        ->limit($limit,$offset)
                        ->order("$orderby DESC");
		
		$data = $this->db->fetchAll($select);		
                return $data;
	}
	return -1;
    }
    public function listAll(){
        $select = $this->db->select()
                ->from($this->_table_name);
                
	$data = $this->db->fetchAll($select);
	return $data;
    }
    public function deleteAllByNewsId($id_news){
        $where = "id_news = ".$id_news;
        return $this->db->delete($this->_table_name,$where);
    }
  
}
?>
